#include "scene.h"
#include "controller/sceneController.h"
#include "controller/watcherController.h"
#include "controller/pilotController.h"
#include "controller/mapController.h"
#include "controller/skyController.h"
#include "controller/driverController.h"
#include "controller/helperController.h"
#include "model/sceneModel.h"
#include "rectangle.h"
#include "view/common_display_utils.h"
#include "view/pilotView.h"
#include "view/sceneView.h"
#include "view/watcherView.h"
#include "view/mapView.h"
#include "view/skyView.h"
#include "view/driverView.h"
#include "view/helperView.h"
#include "model/lighting.h"

Scene::Scene(){
}

Scene::~Scene(){
	for(std::list<Controller*>::iterator it=controllerList.begin();it!=controllerList.end();++it){
		delete *it;
	}
}

void Scene::redisplay(){
	(*this->activeController)->redisplay();

	Framework& fr = Framework::instance();
	if(fr.fpsCalc()){
		std::ostringstream sstr;
		sstr << "FPS : " << (int)fr.fps();
		dynamic_cast<SceneView&>((*this->activeController)->getView()).renderText(sstr.str(),0.75,0.80);
	}
}

void Scene::reshape(int oldW,int oldH,int w,int h){
}

void Scene::timer(int value){
	(*this->activeController)->timer(value);

	Framework& fr = Framework::instance();
	fr.redisplay();
}

void Scene::mousePressed(int key,int state,int x ,int y){
	(*this->activeController)->mousePressed(key,state,x,y);

	Framework::instance().redisplay();
}

void Scene::mouseMotion(int x ,int y){
	(*this->activeController)->mouseMotion(x,y);

	Framework::instance().redisplay();
}

void Scene::specKeyboard(int key,int x ,int y){
	(*this->activeController)->specKeyboard(key,x,y);
}

void Scene::keyboard(unsigned char key,int x ,int y){
	processKey(key);

	(*this->activeController)->keyboard(key,x,y);
}

void Scene::processKey(unsigned char key){
	if (key=='c'){
		++activeController;
		if (activeController==controllerList.end())
			activeController=controllerList.begin();
		(*this->activeController)->notifyActivate();
	}else if(key==0x1b){
		Framework::instance().exit();
	}
}

void Scene::restart(){
	for(std::list<Controller*>::iterator it=controllerList.begin();it!=controllerList.end();++it){
			delete *it;
	}
	init();
	this->activeController=controllerList.begin();
}

void Scene::init(){
	CommonDisplayUtils::instance().createDisplayLists();

	Framework& fr = Framework::instance();
	Rectangle viewport = Rectangle(0,fr.getWindowWidth(),0,fr.getWindowHeight());

	SceneModel* m1 = new SceneModel();

	SceneView* v1 = new WatcherView(viewport,*m1);
	controllerList.push_back(new WatcherController(v1,m1));

	SceneView* v2 = new PilotView(viewport,*m1);
	controllerList.push_back(new PilotController(v2,m1));

	SceneView* v3 = new MapView(viewport,*m1);
	controllerList.push_back(new MapController(v3,m1));

	SceneView* v4 = new SkyView(viewport,*m1);
	controllerList.push_back(new SkyController(v4,m1));

	SceneView* v5 = new DriverView(viewport,*m1);
	controllerList.push_back(new DriverController(v5,m1));

	SceneView* v6 = new HelperView(viewport,*m1);
	controllerList.push_back(new HelperController(v6,m1));

	this->activeController = controllerList.begin();
}
